﻿using Assets.AnimationPlayer.Scripts.Prefabs;
using Assets.AnimationPlayer.Scripts.UI.controls;
using Assets.AnimationPlayer.Scripts.Util;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.AnimationPlayer.Scripts.UI
{
    public class AnimationListSubPage : MonoBehaviour
    {
        public GameObject GameUiMain;
        public TopBarControl TopBarControl;
        public MainContentControl MainContentControl;
        public MyScrollRect ItemListScrollRect;
        public GameObject LoadingView;
        public MessageBox MessageBox;
        public void BindClickEvents(YioksCallBack.CallBackFun backCallBack, YioksCallBack.CallBackOneString playCallBack)
        {
            TopBarControl.BindEvents(backCallBack);
            MainContentControl.BindEvents(playCallBack);
        }

        public void BindMessageBoxEvents(YioksCallBack.CallBackFun okCallBack, YioksCallBack.CallBackFun cancelCallBack1)
        {
            MessageBox.GetComponent<YioksUiAuto>().UiAuto();
            MessageBox.bing_obj("确认要退出" + Application.productName + "？", okCallBack, cancelCallBack1);
        }



        public static int ClumnCount = 1;
        public static int RowsCount = 1;

        public void ResetItemsContainerSize()
        {
            float diy = (RowsCount * 180 + (RowsCount + 1) * 15);
            float dix = (ClumnCount * 280 + (ClumnCount + 1) * 20);

            var containerRtf = ItemListScrollRect.GetComponent<RectTransform>();
            var contentRtf = ItemListScrollRect.content.GetComponent<RectTransform>();

            contentRtf.sizeDelta = new Vector2(dix, diy) * AnimationVar.ScaleNum;
            if (containerRtf.rect.height > diy)
            {
                var oldVector2 = containerRtf.sizeDelta;
                oldVector2.y = contentRtf.sizeDelta.y;
                containerRtf.sizeDelta = oldVector2;
            }
            ItemListScrollRect.Refresh();
            contentRtf.anchoredPosition = new Vector2(contentRtf.anchoredPosition.x, -diy) * AnimationVar.ScaleNum;
        }

        public void UpAnimationItemsStyle(bool isScroll = false, bool fromLeftToRight = false)
        {
            float w = ItemListScrollRect.GetComponent<RectTransform>().sizeDelta.x;
            float dix = ItemListScrollRect.content.GetComponent<RectTransform>().anchoredPosition.x;
            float contentWidth = ItemListScrollRect.content.GetComponent<RectTransform>().sizeDelta.x;
            for (int i = ItemListScrollRect.content.childCount - 1; i >= 0; i--)
            {
                Transform childTransform = ItemListScrollRect.content.GetChild(i);
                if (childTransform && !childTransform.gameObject.name.Equals("none"))
                {
                    if (childTransform.gameObject.name.Equals("animation_" + AnimationVar.AnimationPlayCfg.SelectAnimationIndex))
                    {
                        childTransform.GetComponent<Image>().color = new Color(0, 255, 0, 0.5f);
                        MainContentControl.AnimationItem.SetItem(AnimationVar.AnimationPlayCfg.Cfg.AnimationObjs[AnimationVar.AnimationPlayCfg.SelectAnimationIndex], AnimationVar.AnimationPlayCfg.SelectAnimationIndex,null);
                        if (contentWidth - w <= 0) continue;
                        if (!isScroll)
                        {
                            float offsetX = Mathf.Min(AnimationVar.AnimationPlayCfg.SelectAnimationIndex * 300 * AnimationVar.ScaleNum,
                                contentWidth - w);
                            ItemListScrollRect.ScrollLeft(-offsetX);
                        }
                        else
                        {
                            if (fromLeftToRight)
                            {
                                if (w - dix < (childTransform.GetComponent<AnimationItem>().Index + 1) * 300 * AnimationVar.ScaleNum)
                                {
                                    float offsetX = Mathf.Max((childTransform.GetComponent<AnimationItem>().Index - 2) * 300 * AnimationVar.ScaleNum,
                               0);
                                    ItemListScrollRect.ScrollLeft(-offsetX);
                                }
                            }
                            else
                            {
                                if (-dix > childTransform.GetComponent<AnimationItem>().Index * 300 * AnimationVar.ScaleNum)
                                {
                                    ItemListScrollRect.ScrollLeft(-childTransform.GetComponent<AnimationItem>().Index * 300 * AnimationVar.ScaleNum);
                                }

                            }
                        }
                    }
                    else
                    {
                        childTransform.GetComponent<Image>().color = new Color(255, 255, 255, 0.5f);
                    }
                }
            }
        }


    }


}
